package com.example.concurrentprogramming8016.future.futuretask;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-08 22:02
 */
public class FutureTaskDemo4 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask futureTask1 = new FutureTask(new T2Task2());
        new Thread(futureTask1).start();
        FutureTask futureTask = new FutureTask<>(new T1Task2(futureTask1));
        new Thread(futureTask).start();
        System.out.println(futureTask.get());

    }

}

class T1Task2 implements Callable {
    FutureTask futureTask;

    public T1Task2(FutureTask futureTask) {
        this.futureTask = futureTask;
    }

    @Override
    public Object call() throws Exception {
        System.out.println("洗水壶");
        TimeUnit.SECONDS.sleep(1);
        System.out.println("烧开水");
        TimeUnit.SECONDS.sleep(1);
        System.out.println("泡茶"+futureTask.get());
        return "上茶 "+futureTask.get();
    }
}

class T2Task2 implements Callable {

    @Override
    public Object call() throws Exception {
        System.out.println("洗茶壶");
        TimeUnit.SECONDS.sleep(1);
        System.out.println("洗茶杯");
        TimeUnit.SECONDS.sleep(1);
        System.out.println("拿茶叶");
        TimeUnit.SECONDS.sleep(1);
        return "龙井茶";
    }
}
